//
//  DetailController.m
//  SimpleHappy
//
//  Created by lanou on 15/11/23.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "DetailController.h"
#import "Define.h"


#import "SHCoreData.h"
#import "DetailController.h"

#import "ReserveScrowView.h"

#import "UMSocial.h"
#import "UIImageView+WebCache.h"
#import "UMSocialWechatHandler.h"


#import <BmobSDK/Bmob.h>
#import "SHRegistView.h"
#import "SHLoginView.h"
#import "UMSocialWechatHandler.h"

@interface DetailController ()<UIWebViewDelegate, UMSocialUIDelegate>

@property (nonatomic, strong) UILabel *nowPriceLabel;
@property (nonatomic, strong) UILabel *oldPriceLabel;
@property (nonatomic, strong) UIButton *button; // 预定按钮
@property (nonatomic, strong) UIWebView *webView; // 网页显示

@property (nonatomic, strong) UIButton *shareBtn; // 分享按钮
@property (nonatomic, strong) UIView *myView; // 简介视图


@property (nonatomic, assign) CGFloat headerHeight;

@property (nonatomic, strong) NSString *webUrl; // 网页的链接

@property (nonatomic, strong) UIImage *shareImage; // 用于分享

@end

@implementation DetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];

    // 提前解析要分享的图片
    NSURL *url = [NSURL URLWithString:self.SHDetailModel.picUrl];
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:url];
    self.shareImage = imageView.image;
    // 添加网络视图
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64)];

    // 自动对页面进行缩放以适应屏幕
    self.webView.scalesPageToFit = YES;
    self.webView.backgroundColor = [UIColor whiteColor];
    NSURL *targetUrl = [NSURL URLWithString:self.targetUrl];
    [self.webView loadRequest:[NSURLRequest requestWithURL:targetUrl]];
    self.webView.delegate = self;
    [self.view addSubview:_webView];
    
    
//    CGFloat viewHeight = KScreenWidth / 7.5;
    
    // 添加预定视图
    // 如果不是发现页面跳转的就不加priceView
    if (!_isDiscover) {

        
        // 添加头部视图
        if (self.reserveModel.isPlay == YES) { // 如果是玩家页面
           self.headerHeight = KScreenHeight / 2.5;
        }else{
            self.headerHeight = KScreenHeight / 1.45;
        }
        
        ReserveScrowView *headerView = [[ReserveScrowView alloc]initWithFrame:CGRectMake(0, - self.headerHeight, KScreenWidth, self.headerHeight)];
        if (self.reserveModel.isPlay == YES) {
            headerView.isPlay = YES;
        }
        
        headerView.model = self.reserveModel;
        [self.webView.scrollView addSubview:headerView];
        self.webView.scrollView.contentInset = UIEdgeInsetsMake(self.headerHeight, 0, 0, 0);
    }

//    CGFloat twenty = KScreenWidth / 18.75;
    CGFloat tewntyFive = KScreenWidth / 15;
    CGFloat thrity = KScreenWidth / 12.5;
    // 创建返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, tewntyFive+10, tewntyFive+10);
    [backButton setImage:[UIImage imageNamed:@"zuo"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backBarButton;
    // 右上角2个button
    
    // 分享按钮
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareBtn.frame = CGRectMake(0, 0, thrity, thrity);
    [self.shareBtn setImage:[UIImage imageNamed:@"fenxiang"] forState:UIControlStateNormal];
    [self.shareBtn addTarget:self action:@selector(shareBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *shareBarBtn = [[UIBarButtonItem alloc]initWithCustomView:_shareBtn];
    
    // 心愿按钮
    if (self.isHead == NO) {
        self.wishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.wishBtn.frame = CGRectMake(0, 0, thrity, thrity);
        
        NSString *title = [SHCoreData findWithTitle:self.SHDetailModel.title];
        if (![self.SHDetailModel.title isEqualToString:title]) {
            [self.wishBtn setImage:[UIImage imageNamed:@"aixin"] forState:UIControlStateNormal];
        }
        else
        {
            [self.wishBtn setImage:[UIImage imageNamed:@"aixin2"] forState:UIControlStateNormal];
        }
        
        [self.wishBtn addTarget:self action:@selector(wishBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *wishBarBtn = [[UIBarButtonItem alloc]initWithCustomView:_wishBtn];
        // 添加两个button
        self.navigationItem.rightBarButtonItems = @[shareBarBtn,wishBarBtn];

    }
    else
    {
        self.navigationItem.rightBarButtonItem = shareBarBtn;
    }
   
    
    

}

// 心愿按钮方法
- (void)wishBtnAction:(UIButton *)button
{
//////////判断是否登陆
    BmobUser *user = [BmobUser getCurrentUser];
    if (user) {
        // 已登陆
        NSString *title = [SHCoreData findWithTitle:self.SHDetailModel.title];
        if ([self.SHDetailModel.title isEqualToString:title]) {
            // 已收藏
            [SHCoreData deleteModel:self.SHDetailModel.title];
            [self.wishBtn setImage:[UIImage imageNamed:@"aixin"] forState:UIControlStateNormal];
        }
        else
        {
            // 未收藏
            [SHCoreData addModel:self.SHDetailModel];
            [self.wishBtn setImage:[UIImage imageNamed:@"aixin2"] forState:UIControlStateNormal];
        }
        
    }else{  // 没有登陆
        [self noLogin];
    }
    
}


// 未登录执行的方法
- (void)noLogin
{
    // 警告框
    UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"请先登陆！" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"先看看" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *goToRegist = [UIAlertAction actionWithTitle:@"去登陆" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 登陆失败跳转到注册页面
        SHLoginView *regitsh = [[SHLoginView alloc]init];
        [self presentViewController:regitsh animated:YES completion:nil];
    }];
    [alertController addAction:goToRegist];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

}

// 分享按钮方法
- (void)shareBtnAction:(UIButton *)button
{
    [UMSocialWechatHandler setWXAppId:@"wxaae072fcd1c171fa" appSecret:@"d4624c36b6795d1d99dcf0547af5443d" url:self.SHDetailModel.openUrl];
    
    NSString *str = [NSString stringWithFormat:@"%@ \n %@ \n分享自 SimpleHappy简悦", self.SHDetailModel.title, self.SHDetailModel.openUrl];
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"565916c367e58ec04b0077f1" shareText:str
                                     shareImage:self.shareImage
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToEmail,UMShareToSms, nil]
                                       delegate:nil];
}

// 返回按钮方法
- (void)backButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}


// 判断网页点击  // https://m.yhouse.com/meal-app/815
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

    // 获取点击的字符串
    NSString *str = request.URL.absoluteString;
//    NSLog(@"str     %@",str);
    // 截取指定字符串之后的字符串
    if (![str containsString:@"https://m.yhouse"]  ) {
        // 如果是美食跳转过来的
        if ([str containsString:@"meal/"]) {
            NSString *linkStr = [str componentsSeparatedByString:@"meal/"].lastObject;
            // 拼接字符串
            self.webUrl = [NSString stringWithFormat:@"https://m.yhouse.com/meal-app/%@",linkStr];
//            NSLog(@"linkStr------%@",linkStr);
//            NSLog(@"webUrl------%@",self.webUrl);
        }
        // 如果是生活跳转过来的
        else if ([str containsString:@"event/"])
        {
            NSString *linkStr = [str componentsSeparatedByString:@"event/"].lastObject;
            // 拼接字符串
            self.webUrl = [NSString stringWithFormat:@"https://m.yhouse.com/event-app/%@",linkStr];
        }
        else
        {
//            NSLog(@"跳转错误");
            return YES;
        }
        
        DetailController *detailVc = [[DetailController alloc]init];
        detailVc.isDiscover = YES;
        detailVc.targetUrl = self.webUrl;
        [self.navigationController pushViewController:detailVc animated:YES];
    }
    return YES;
}
 

// 网页开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{



}

// 网页加载完毕
- (void)webViewDidFinishLoad:(UIWebView *)webView
{



}

// 网页错误
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{
    
}

@end

